package br.com.sovi.gwt.client.controleunitario;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.sovi.gwt.client.entity.ControleUnitario;
import br.com.sovi.gwt.client.entity.ControleUnitarioPoint;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Node;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;

public class ControleUnitarioMontanhismoMenuPanel extends TreePanel {

	private SelectionListener selectionListener;

	public ControleUnitarioMontanhismoMenuPanel() {
		this.setTitle("Rastreamento em Trilha");
		this.setHeight("100%");
		this.setWidth(200);
		this.setCollapsed(true);
		this.setAnimate(true);
		this.setEnableDD(true);
		this.setContainerScroll(true);
		this.setAutoScroll(true);
		this.setRootVisible(false);
		this.setFrame(true);

		this.expand();

		populateTree();
	}

	private void populateTree() {
		List<ControleUnitario> points = getControleUnitarioList();

		TreeNode rootNode = new TreeNode();

		for (final ControleUnitario controleUnitario : points) {
			String title = controleUnitario.getDescricao();
			if (title == null) {
				DateTimeFormat.getShortDateFormat().format(controleUnitario.getData());
			}

			TreeNode treeNode = new TreeNode(title);
			treeNode.addListener(new TreeNodeListenerAdapter() {
				@Override
				public void onClick(Node node, EventObject e) {
					if (getSelectionListener() != null) {
						getSelectionListener().onClick(controleUnitario);
					}
				}
			});

			rootNode.appendChild(treeNode);
		}

		this.setRootNode(rootNode);
	}

	private List<ControleUnitario> getControleUnitarioList() {
		List<ControleUnitario> points = new ArrayList<ControleUnitario>();

		points.add(new ControleUnitario("Porto Belo - SC", createDatePlusMinutes(1, 0), false, true, 11,
				new ControleUnitarioPoint[] {
						new ControleUnitarioPoint(createDatePlusMinutes(1, 0), -27.147221, -48.532834),
						new ControleUnitarioPoint(createDatePlusMinutes(1, 1), -27.152221, -48.525834),
						new ControleUnitarioPoint(createDatePlusMinutes(1, 2), -27.158830, -48.520834),
						new ControleUnitarioPoint(createDatePlusMinutes(1, 3), -27.160830, -48.519834), }));

		points.add(new ControleUnitario("Rio no Pará", createDatePlusMinutes(1, 0), false, true, 11,
				new ControleUnitarioPoint[] {
						new ControleUnitarioPoint(createDatePlusMinutes(2, 0), -8.552164, -52.508469),
						new ControleUnitarioPoint(createDatePlusMinutes(2, 1), -8.53451, -52.555847),
						new ControleUnitarioPoint(createDatePlusMinutes(2, 2), -8.436376, -52.636871),
						new ControleUnitarioPoint(createDatePlusMinutes(2, 3), -8.286919, -52.627602), }));

		return points;
	}

	private Date createDatePlusMinutes(int day, int minutes) {
		return new Date(2009, 2, day, 10, minutes, 0);
	}

	public void setSelectionListener(SelectionListener selectionListener) {
		this.selectionListener = selectionListener;
	}

	public SelectionListener getSelectionListener() {
		return selectionListener;
	}

	public static interface SelectionListener {
		void onClick(ControleUnitario controleUnitario);
	}
}
